layui.use(['table', 'layer'], function () {
    var table = layui.table;
    var layer = layui.layer;

    var applyTable = table.render({
        elem: '#applyTable'
        // ,height: 315
        // ,url: baseURL + 'sys/branch/list' //数据接口
        , page: true //开启分页


        , cols: [[ //表头
            {field: 'branchCode', title: '工单编号', sort: true, fixed: 'left'}
            , {field: 'branchName', title: '商户名称', minWidth: '100'}
            , {field: 'type', title: '工单类型', minWidth: '90'}
            , {field: 'time', title: '派单时间', minWidth: '90'}
            , {field: 'bizManager', title: '责任人', minWidth: '90'}
            , {field: 'status', title: '状态', minWidth: '90'}
            , {fixed: 'right', width: 178, title: '操作',align: 'center', toolbar: '#barDemo'}
        ]]

        , data: [{
            "branchCode": "10001"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10002"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10003"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10004"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10005"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10006"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10007"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10008"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }, {
            "branchCode": "10009"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        },  {
            "branchCode": "10010"
            , "branchName": "恒生电子股份有限公司"
            ,"type":"正常"
            , "time": "2017-12-11 09:55"
            , "bizManager": "唐宏轩"
            , "status": "待审"
        }]

    });



    $("#sysBranchQueryPage").click(function () {

        var $sysBranchQueryForm = $('#sysBranchQueryForm');

        applyTable.reload({
            where: { //设定异步数据接口的额外参数，任意设
                branchCode: $sysBranchQueryForm.find('select[name=branchCode]').val()
                , branchLevel: $sysBranchQueryForm.find('select[name=branchLevel]').val()
                , parentBranch: $sysBranchQueryForm.find('select[name=parentBranch]').val()
                , status: $sysBranchQueryForm.find('select[name=status]').val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });

    });

    //监听工具条
    table.on('tool(branchTableFilter)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'detail') { //查看
            window.location = '../apply/send-detail.html'
            //$.post('merchant-detail.html', function (data) {
            //    layer.open({
            //        type: 1,
            //        area: ['1000px', '460px'],
            //        content: data
            //    });
            //}, 'html');
        } else if (layEvent === 'cancle') { //驳回
            layer.confirm('真的要取消吗?', function (index) {

                layer.close(index);
                //向服务端发送删除指令
            });
        } else if (layEvent === 'edit') { //编辑
            //do something

            //同步更新缓存对应的值
            obj.update({
                username: '123'
                , title: 'xxx'
            });
        }
    });
});